1 Imports System.Data.OleDb
2 Public Class frmCategory
3
4 Sub Reset()
5 txtCategory.Text = ""
6 txtVAT.Text = ""
7 txtSCPer.Text = ""
8 txtServiceTax.Text = ""
9 txtCategory.Focus()
10 btnSave.Enabled = True
11 btnUpdate.Enabled = False
12 btnDelete.Enabled = False
13 End Sub
14 Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
15 Me.Close()
16 End Sub
17
18 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
19 If txtCategory.Text = "" Then
20 MessageBox.Show("Please enter category name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
21 txtCategory.Focus()
22 Return
23 End If
24
25 Try
26 con = New OleDbConnection(cs)
27 con.Open()
28 Dim ct As String = "select CategoryName from Category where CategoryName=@d1"
29 cmd = New OleDbCommand(ct)
30 cmd.Connection = con
31 cmd.Parameters.AddWithValue("@d1", txtCategory.Text)
32 rdr = cmd.ExecuteReader()
33
34 If rdr.Read() Then
35 MessageBox.Show("Category Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
36 txtCategory.Text = ""
37 txtCategory.Focus()
38 If (rdr IsNot Nothing) Then
39 rdr.Close()
40 End If
41 Return
42 End If
43
44 con = New OleDbConnection(cs)
45 con.Open()
46
47 Dim cb As String = "insert into Category(CategoryName,VAT,ST,SC) VALUES (@d1," & txtVAT.Text & "," & txtServiceTax.Text & "," & txtSCPer.Text & ")"
48 cmd = New OleDbCommand(cb)
49 cmd.Connection = con
50 cmd.Parameters.AddWithValue("@d1", txtCategory.Text)
51 cmd.ExecuteReader()
52 con.Close()
53 Dim st As String = "added the new category '" & txtCategory.Text & "'"
54 LogFunc(lblUser.Text, st)
55 MessageBox.Show("Successfully saved", "Category", MessageBoxButtons.OK, MessageBoxIcon.Information)
56 Getdata()
57 Catch ex As Exception
58 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
59 End Try
60 End Sub
61
62 Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
63 Try
64 If MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
65 DeleteRecord()
66 End If
67 Catch ex As Exception
68 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
69 End Try
70 End Sub
71 Private Sub DeleteRecord()
72
73 Try
74
75 Dim RowsAffected As Integer = 0
76 con = New OleDbConnection(cs)
77 con.Open()
78 Dim cl As String = "select CategoryName from Dish,Category where Dish.Category=Category.CategoryName and CategoryName=@d1"
79 cmd = New OleDbCommand(cl)
80 cmd.Connection = con
81 cmd.Parameters.AddWithValue("@d1", txtCategory.Text)
82 rdr = cmd.ExecuteReader()
83 If rdr.Read Then
84 MessageBox.Show("Unable to delete..Already in use in Menu Item Entry", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
85 If Not rdr Is Nothing Then
86 rdr.Close()
87 End If
88 Exit Sub
89 End If
90 con = New OleDbConnection(cs)
91 con.Open()
92 Dim cq As String = "delete from Category where CategoryName=@d1"
93 cmd = New OleDbCommand(cq)
94 cmd.Connection = con
95 cmd.Parameters.AddWithValue("@d1", txtCategory.Text)
96 RowsAffected = cmd.ExecuteNonQuery()
97 If RowsAffected > 0 Then
98 Dim st As String = "deleted the category '" & txtCategory.Text & "'"
99 LogFunc(lblUser.Text, st)
100 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
101 Getdata()
102 Reset()
103 Else
104 MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
105 Reset()
106 End If
107 If con.State = ConnectionState.Open Then
108 con.Close()
109
110 End If
111 Catch ex As Exception
112 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
113 End Try
114 End Sub
115
116 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
117 Try
118 If txtCategory.Text = "" Then
119 MessageBox.Show("Please enter category name", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
120 txtCategory.Focus()
121 Return
122 End If
123 con = New OleDbConnection(cs)
124 con.Open()
125 Dim cb As String = "update Category set CategoryName=@d1,VAT=" & txtVAT.Text & ",ST=" & txtServiceTax.Text & ",SC=" & txtSCPer.Text & " where CategoryName=@d2"
126 cmd = New OleDbCommand(cb)
127 cmd.Connection = con
128 cmd.Parameters.AddWithValue("@d1", txtCategory.Text)
129 cmd.Parameters.AddWithValue("@d2", txtCategoryName.Text)
130 cmd.ExecuteReader()
131 con.Close()
132 Dim st As String = "updated the category '" & txtCategory.Text & "' details"
133 LogFunc(lblUser.Text, st)
134 MessageBox.Show("Successfully updated", "category Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
135 btnUpdate.Enabled = False
136 Getdata()
137 Catch ex As Exception
138 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
139 End Try
140 End Sub
141 Public Sub Getdata()
142 Try
143 con = New OleDbConnection(cs)
144 con.Open()
145 cmd = New OleDbCommand("SELECT RTRIM(CategoryName), (VAT), (ST),SC from Category order by CategoryName", con)
146 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
147 dgw.Rows.Clear()
148 While (rdr.Read() = True)
149 dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3))
150 End While
151 con.Close()
152 Catch ex As Exception
153 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
154 End Try
155 End Sub
156 Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
157 Reset()
158 End Sub
159
160
161 Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
162 Dim strRowNumber As String = (e.RowIndex + 1).ToString()
163 Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
164 If dgw.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then
165 dgw.RowHeadersWidth = Convert.ToInt32((size.Width + 20))
166 End If
167 Dim b As Brush = SystemBrushes.ControlText
168 e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
169
170 End Sub
171
172 Private Sub frmCategory_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
173 If e.KeyCode = Keys.Enter Then
174 Me.SelectNextControl(Me.ActiveControl, True, True, True, False) 'for Select Next Control
175 End If
176 End Sub
177
178 Private Sub frmCategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
179 Getdata()
180 End Sub
181
182 Private Sub dgw_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
183 Try
184 If dgw.Rows.Count > 0 Then
185 Dim dr As DataGridViewRow = dgw.SelectedRows(0)
186 txtCategory.Text = dr.Cells(0).Value.ToString()
187 txtCategoryName.Text = dr.Cells(0).Value.ToString()
188 txtVAT.Text = dr.Cells(1).Value.ToString()
189 txtServiceTax.Text = dr.Cells(2).Value.ToString()
190 txtSCPer.Text = dr.Cells(3).Value.ToString()
191 btnUpdate.Enabled = True
192 btnDelete.Enabled = True
193 btnSave.Enabled = False
194 End If
195 Catch ex As Exception
196 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
197 End Try
198 End Sub
199
200 Private Sub txtVAT_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtVAT.KeyPress
201 Dim keyChar = e.KeyChar
202
203 If Char.IsControl(keyChar) Then
204 'Allow all control characters.
205 ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
206 Dim text = Me.txtVAT.Text
207 Dim selectionStart = Me.txtVAT.SelectionStart
208 Dim selectionLength = Me.txtVAT.SelectionLength
209
210 text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
211
212 If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
213 'Reject an integer that is longer than 16 digits.
214 e.Handled = True
215 ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
216 'Reject a real number with two many decimal places.
217 e.Handled = False
218 End If
219 Else
220 'Reject all other characters.
221 e.Handled = True
222 End If
223 End Sub
224
225 Private Sub txtServiceTax_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtServiceTax.KeyPress
226 Dim keyChar = e.KeyChar
227
228 If Char.IsControl(keyChar) Then
229 'Allow all control characters.
230 ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
231 Dim text = Me.txtServiceTax.Text
232 Dim selectionStart = Me.txtServiceTax.SelectionStart
233 Dim selectionLength = Me.txtServiceTax.SelectionLength
234
235 text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
236
237 If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
238 'Reject an integer that is longer than 16 digits.
239 e.Handled = True
240 ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
241 'Reject a real number with two many decimal places.
242 e.Handled = False
243 End If
244 Else
245 'Reject all other characters.
246 e.Handled = True
247 End If
248 End Sub
249
250 Private Sub txtSCPer_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtSCPer.KeyPress
251 Dim keyChar = e.KeyChar
252
253 If Char.IsControl(keyChar) Then
254 'Allow all control characters.
255 ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
256 Dim text = Me.txtSCPer.Text
257 Dim selectionStart = Me.txtSCPer.SelectionStart
258 Dim selectionLength = Me.txtSCPer.SelectionLength
259
260 text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
261
262 If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
263 'Reject an integer that is longer than 16 digits.
264 e.Handled = True
265 ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
266 'Reject a real number with two many decimal places.
267 e.Handled = False
268 End If
269 Else
270 'Reject all other characters.
271 e.Handled = True
272 End If
273 End Sub
274
275 End Class